<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" import="web_hr.*,java.sql.*,java.util.*,java.io.*,java.text.ParseException;"
    pageEncoding="UTF-8"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META HTTP-EQUIV="pragma" CONTENT="no-cache"> 
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate"> 
<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT"> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="../Res/Bootstrap/css/bootstrap.min.css">
<script src="../Res/JQuery/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
 		$(".calendar").click(function(){
			var day = $(this).text();
			$.ajax({
				type:"post",
				url:"../CheckDay",
				data:{day:eval(day),name:$("#inputname").val()},
				success:function(data,textStatus){
					alert(data);	
				}
			});
		}); 
	});
</script>
<style>
/* 让鼠标移动到上面的时候变为手型 */
.calendar{
	cursor:pointer;
}
</style>
<title>Insert title here</title>
</head>
<body class="container">
	<table class="table">
		<thead>
			<tr>
				<th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th>
				<th>Thu</th><th>Fri</th><th>Sat</th>
			</tr>
		</thead>
		<tbody>
		<%
		PrintWriter pw = response.getWriter();
		//System.out.println("start");
/* 		//获取本月签到数据
		String username = (String)request.getParameter("username");
		System.out.println(username);
		Model time = new Model("hr_time");
		String sql = "select time from hr_time where username = '"+username+"' order by sta";
		ResultSet rs = time.eQuery(sql); */

		//获取本月第一天周几，结果类似“星期五”
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//"F"月份中的星期，比如说这个月的 某一天对应星期几
		SimpleDateFormat sdf1 = new SimpleDateFormat("F");
		Calendar calendar = Calendar.getInstance();
		int mon = calendar.get(Calendar.MONTH)+1; 
		int year = calendar.get(Calendar.YEAR); 
		//拼凑每个月第一天的日期字符串，格式为"yyyy-MM-dd"；
		String str=null;
		if(mon<10){
			str = year+"-0"+mon+"-"+"01";
		}else{
			str = year+"-"+mon+"-"+"01";
		}
		//获取本月最后一天
		int maxDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		java.util.Date firstDay = null;
		try {
			// parse()从字符串进行解析的 Date。如果发生错误，则返回 null
			firstDay = sdf.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//获得每个月的第一天是星期几，返回值类型为string ,内容为number，比如周二，返回值为"2";
		String week = sdf1.format(firstDay);
		//System.out.println(week);
		//将week转换为int 型，是为了确定每个月的第一天的位置，即 确定在每个月的1号之前空多少个格子；
		int i = Integer.parseInt(week);
		//flage =1，在日历表中 即 1号对应数字1,
		int flag=1;
		
		int count =0;
%>
			<tr>
<%      //利用 i 的值来确定每个月的1 号前空的空格数；
			for(int j=0;j<i;j++){			
%>
				<td> </td>
<% 
				count++;
			}
			if(count%7==0){ 
%>
				</tr><tr>
<% 
			}
			
		Model time1 = new Model("hr_time");
		//String name = "admin";
		
		//从 salary。jsp里 getgetParameter("salaryName"),获得 name ；
		
		String name = (String)request.getParameter("salaryName");
		//System.out.println("8011"+salaryName);
		//String name=(String)request.getSession().getParameter("salaryName");
		String theDay = null;
		String day =null;
		//flage 小于等于每个月的日期最大值；
		while(flag<=maxDay){
			//判断该日签到情况  
			if(flag<10){
				day = "0"+flag; 
			}else{
				day = "" +flag;
			}
			if(mon<10){
				theDay = year+"-0"+mon+"-"+day;
			}else{
				theDay = year+"-"+mon+"-"+day;
			}
			
			//java.util.Date td = new java.util.Date("2008-04-14");
			//System.out.println(td);sdf.f
			//String search = "select time from hr_time where sta between to_date('"+sdf.format(td)+" 00:00:00','YYYY-MM-DD HH24:MI:SS') and to_date('"+sdf.format(td)+" 23:59:59','YYYY-MM-DD HH24:MI:SS') and username='"+name+"'";
			
			//获取每天签到的时间差，计算每天工作的时间，根据时间 判断 该天在日历上显示的颜色。
			String search = "select time,sta,end from hr_time where sta between to_date('"+theDay+" 00:00:00','YYYY-MM-DD HH24:MI:SS') and to_date('"+theDay+" 23:59:59','YYYY-MM-DD HH24:MI:SS') and username='"+name+"'";
			ResultSet rs = time1.eQuery(search);
			String ff = null;
			if(rs.next()){
				if(rs.getInt(1)!=0){
					int hours = rs.getInt(1)/60;
					if(hours>=8){
						ff="green";
					}else if(hours>=4){
						ff="yellow";
					}else{
						ff="red";
					}
				}else{
					ff="red";
				}
			}else{
				ff="white";
			}
		
%>       
<!--  将判断得到的颜色ff 后，再设置日历上对应的  flag的  的背景颜色为ff;-->
			<td class="calendar" style="background-color:<%=ff%>"><%=flag%></td>
<% 
			flag++;
			count++;
		
			if(count%7==0){ 
%>
				</tr><tr>
<%
			}
		} 
%>
</tr>
		</tbody>
	</table>
	<input type="hidden" id="inputname" value="<%=name %>">
</body>
</html>
